// pages/bindMobile/bindMobile.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_list:{},//用户信息
    pass_old:'',//老秘吗
    ipone: '',//密码
    ipones: '',//确认密码
    ispassword:"请输入密码",
    ispassword_2:"确认输入密码",
  },


  iponeInput(data) {
    this.setData({
      ipone: data.detail.value
    })
  },
  iponeInputs(data) {
    this.setData({
      ipones: data.detail.value
    })
  },
  iponeInputs_old(data) {
    this.setData({
      pass_old: data.detail.value
    })
  },

  // 绑定手机或换绑
  bindMobile() {
    if (!this.data.ipone) {
      wx.showToast({
        title: "密码不能为空",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!this.data.ipones) {
      wx.showToast({
        title: "请输入确认密码",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.ipones != this.data.ipone) {
      wx.showToast({
        title: "俩次密码不一致",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.user_list.password == 1 && !this.data.pass_old) {
      wx.showToast({
        title: "原密码不正确",
        icon: 'none',
        duration: 1000
      })
      return
    }
    let _ = this;
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: app.globalData.user_id,
      phone: _.data.ipone,
      newPassword:_.data.ipones,
      time: timestamp,
      token: app.globalData.userToken,
      oldPassword:_.data.pass_old,
    };
    let _token = axios.encryption(arys);
    let params = {
      phone: _.data.ipone,
      userId: app.globalData.user_id,
      newPassword:_.data.ipones,
      time: timestamp,
      token: app.globalData.userToken,
      oldPassword:_.data.pass_old,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/updatePass",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 1000
        })
        var t;
        clearTimeout(t);
        t = setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500);
     
        // wx.navigateTo({
        //   url: '/pages/login/login',
        // })
        // this.setData({
        //   ipone:'',
        //   ipones:'',
        //   pass_old:''
        // })
      }else{
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 2000
        })
      }

      // console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_list = wx.getStorageSync('UserInformation')
    if(user_list.password == 1){
      this.setData({
        ispassword:"输入新密码",
        ispassword_2:"确认新密码",
      })
    }
    this.setData({
      user_list:user_list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})